const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
var loginStatus = wx.getStorageSync("loginStatus");
const {
  $Toast
} = require('../../components/base/base');


Page({

  data: {
    navBg: app.globalData.navBg,
    navColor: app.globalData.navColor,
    windowHeight: app.globalData.windowHeight - app.globalData.navHeight,
    navTop: app.globalData.navHeight,
    back: true,
    navBox: true,
    home: true,
    scrollHeight: app.globalData.windowHeight - app.globalData.navHeight,
    goTop: true
  },

  newEdit: function(e) {
    util.navigateTo('../newaddress/newaddress')
  },

  randomn(n) {
    if (n > 21)
      return null
    return parseInt((Math.random() + 1) * Math.pow(10, n - 1))
  },

  toFirst(fieldData, index) {
    if (index != 0) {
      fieldData.unshift(fieldData.splice(index, 1)[0]);
    }
  },

  radioWarp(e) {
    var that = this;
    var arr = wx.getStorageSync('contactormodel') || [];
    var index = e.currentTarget.dataset.index;
    for (var i = 0; i < arr.length; i++) {
      arr[i].isDefault = false;
      if (arr[i].id == index) {
        arr[i].isDefault = true;
      }
    }
    wx.setStorage({
      key: 'contactormodel',
      data: arr,
      success: function(){
        that.onShow()
      }
    })
    
  },

  wxEdit: function() {
    var that = this;
    var arr = wx.getStorageSync('contactormodel') || [];
    wx.chooseAddress({
      success: function(res) {
        var contactormodel = {
          userName: res.userName,
          mobile: res.telNumber,
          areainfo: res.provinceName + res.cityName + res.countyName,
          address: res.detailInfo,
          isDefault: false,
          id: that.randomn(6)
        };
        arr.push(contactormodel);
        wx.setStorageSync('contactormodel', arr);
      }

    })
  },

  deladdress: function(e) {
    var that = this;
    var arr = wx.getStorageSync('contactormodel') || [];
    var idx = e.currentTarget.dataset.idx;
    var newallData = [];
    for (var i in arr) {
      var item = arr[i];
      if (item.id != idx) {
        newallData.push(item);
      }
    }
    
    wx.showModal({
      title: '',
      confirmText: "删除",
      confirmColor: "#e93b3d",
      cancelText: "取消",
      cancelColor: "#333",
      content: '是否确认删除当前地址',
      success: function(res) {
        if (res.confirm) {
          for (var i = 0; i < newallData.length; i++) {
            if (newallData[i].isDefault == true) {
              that.toFirst(newallData, i)
            }
          }
          wx.setStorage({
            key: 'contactormodel',
            data: newallData,
            success: function () {
              that.setData({
                contactormodel: newallData
              })
            }
          })
          
        } else if (res.cancel) {

        }
      }
    })
  },

  exitArea: function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    util.navigateTo('../editaddress/editaddress?id=' + id)
  },

  scroll: function(e) {
    var that = this;
    var listIndex = (e.detail.scrollTop + this.data.screenHeight) / (this.data.screenWidth * 0.4)
    if (e.detail.scrollTop > app.globalData.navHeight + 40) {
      this.setData({
        goTop: false
      });
      this.setData({
        listIndex: listIndex
      })
    } else {
      this.setData({
        goTop: true
      });
    }

  },

  //返回顶部
  goScrolltop: function(e) {
    var that = this;
    var top = that.data.scrollTop;
    if (top == 1) {
      top = 0;
    } else {
      top = 1;
    }

    that.setData({
      scrollTop: top
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var arr = wx.getStorageSync('contactormodel') || [];

    for (var i = 0; i < arr.length; i++) {
      if (arr[i].isDefault == true) {
        that.toFirst(arr, i)
      }
    }
    wx.request({
      url: util.api + api.GetAddressList,
      data: {
        BUSINESS_PARAMETERS: {
          userToekn: loginStatus.token,
          PostStaId: 22
        },
        SYSTEM_PARAMETERS: {
          APP_ID: "M_P_G001",
          METHOD_ID: 0,
          ACCESS_TOKEN: "1w2Re3bE^og38",
          TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
          SIGN: "string"
        }
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          contactormodel: res.data.RESULT
        });
        console.log(res.data.RESULT)
      },
      fail: function (res) {
        
      }

    })
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }

})